package com.base_java.service.impl;

import com.base_java.common.Const;
import com.base_java.common.ServerResponse;
import com.base_java.dao.DeviceMapper;
import com.base_java.pojo.Device;
import com.base_java.service.IDeviceService;
import com.base_java.util.LongIdGenerator;
import com.base_java.vo.DeviceListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("iDeviceService")
public class DeviceServiceImpl implements IDeviceService {
    private LongIdGenerator generator = new LongIdGenerator(1L);

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public ServerResponse<String> add(Device device) {
        // todo 代码重复较多，后期请采用反射优化
        if (StringUtils.isBlank(device.getMacId())) {
            return ServerResponse.createByErrorMessage("参数错误，需要传递macId");
        }
//        ServerResponse<String> validResponse =this.checkValid(device.getMacId(),Const.MACID);
//        ServerResponse<String> validResponse =CheckValidService.getInstance().checkValid(device.getMacId(),Const.MACID);

//        if (!validResponse.isSuccess()) {
//            return validResponse;
//        }
        if (StringUtils.isBlank(device.getLocation())) {
            return ServerResponse.createByErrorMessage("参数错误，需要传递location");
        }
        if (StringUtils.isBlank(device.getBrand())) {
            return ServerResponse.createByErrorMessage("参数错误，需要传递brand");
        }
        if (StringUtils.isBlank(device.getModel())) {
            return ServerResponse.createByErrorMessage("参数错误，需要传递model");
        }
        if (device.getHotelId() == null) {
            return ServerResponse.createByErrorMessage("参数错误，需要传递hotelId");
        }
        Device updateDevice = new Device();
        updateDevice.setMacId(device.getMacId());
        updateDevice.setUuid(generator.generate());
        updateDevice.setLocation(device.getLocation());
        updateDevice.setBrand(device.getBrand());
        updateDevice.setModel(device.getModel());
        updateDevice.setMacSystem(device.getMacSystem());
        updateDevice.setVersion(device.getVersion());
        updateDevice.setLastIp(device.getLastIp());
        updateDevice.setExtra(device.getExtra());
        updateDevice.setType(device.getType());
        updateDevice.setHotelId(device.getHotelId());
        updateDevice.setUpdateTime(new Date());
        updateDevice.setCreateTime(new Date());
        int resultCount = deviceMapper.insertDevice(updateDevice);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("添加设备失败");
        }
        return ServerResponse.createBySuccessMessage("添加设备成功");
    }

    public ServerResponse<String> checkValid(String str, String tybe) {
        if (StringUtils.isNotBlank(tybe)) {
            if (Const.MACID.equals(tybe)) {
                int resultCount = deviceMapper.checkMacId(str);
                if (resultCount > 0) {
                    return ServerResponse.createByErrorMessage("macId已存在");
                }
            }
        } else {
            return ServerResponse.createByErrorMessage("校验的type不能为空");

        }
        return ServerResponse.createBySuccessMessage("校验成功");
    }

    @Override
    public ServerResponse<PageInfo> deviceList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Device> deviceList = deviceMapper.selectList();
        List<DeviceListVo> deviceListVoList = Lists.newArrayList();
        for (Device deviceItem : deviceList) {
            DeviceListVo deviceListVo = assembleDeviceListVo(deviceItem);
            deviceListVoList.add(deviceListVo);
        }
        PageInfo pageResult = new PageInfo(deviceList);
        pageResult.setList(deviceListVoList);
        return ServerResponse.createBySuccess(pageResult);

    }

    private DeviceListVo assembleDeviceListVo(Device device) {
        DeviceListVo deviceListVo = new DeviceListVo();
        deviceListVo.setId(device.getId());
        deviceListVo.setMacId(device.getMacId());
        deviceListVo.setLocation(device.getLocation());
        deviceListVo.setBrand(device.getBrand());
        deviceListVo.setModel(device.getModel());
        deviceListVo.setHotelId(device.getHotelId());
        return deviceListVo;

    }

    @Override
    public ServerResponse<String> move(Integer hotelId) {
        if (hotelId == null) {
            return ServerResponse.createByErrorMessage("请输入要删除的房间号");
        }
        int resultCount = deviceMapper.moveDevice(hotelId);
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMessage("删除设备成功");
        }
        return ServerResponse.createByErrorMessage("该房间的设备不存在，无需删除");
    }

    @Override
    public ServerResponse<Device> updateInformation(Device device) {
        //    先查下这个设备是否存在
        Device updateDevice = new Device();
        updateDevice.setMacId(device.getMacId());
        updateDevice.setUuid(device.getUuid());
        updateDevice.setLocation(device.getLocation());
        updateDevice.setBrand(device.getBrand());
        updateDevice.setModel(device.getModel());
        updateDevice.setMacSystem(device.getMacSystem());
        updateDevice.setVersion(device.getVersion());
        updateDevice.setLastIp(device.getLastIp());
        updateDevice.setExtra(device.getExtra());
        updateDevice.setType(device.getType());
        updateDevice.setHotelId(device.getHotelId());
        updateDevice.setUpdateTime(device.getUpdateTime());

        int updateCount = deviceMapper.updateById(updateDevice);
        if (updateCount > 0) {
            return ServerResponse.createBySuccess("更新设备信息成功", updateDevice);
        }
        return ServerResponse.createByErrorMessage("更新设备信息失败");

    }


}
